package org.ihelpuoo.weboo.security;

import java.util.HashMap;
import java.util.Map;

import org.ihelpuoo.weboo.exception.DispathException;
import org.ihelpuoo.weboo.module.ActionContext;
import org.ihelpuoo.weboo.module.IController;

public abstract class SessionController<T> implements IController
{

	protected String			configName;
	private Map<String, T[]>	map;

	public SessionController()
	{
		this("session.control");
	}

	public SessionController(String configName)
	{
		map = new HashMap<String, T[]>();
		this.configName = configName;
	}

	public String dispatch(ActionContext context) throws Exception
	{
		if (!iNeedDoCheck(context))
			return null;

		T[] items = map.get(context.getModuleContext().getModule().getName());
		if (null == items)
		{
			synchronized (this)
			{
				items = map.get(context.getModuleContext().getModule().getName());
				if (null == items)
				{
					String v = context.getModuleContext().properties().getString(configName);
					items = parseConifgItem(context,v);
					map.put(context.getModuleContext().getModule().getName(),items);
				}
			}
		}
		if (items.length == 0)
			return null;
		return doCheck(context,items);
	}

	protected boolean iNeedDoCheck(ActionContext context) throws DispathException
	{
		return true;
	}

	protected abstract T[] parseConifgItem(ActionContext context, String value);

	protected abstract String doCheck(ActionContext context, T[] items) throws Exception;
}
